package com.yj.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.yj.config.CloudStorageConfig;

public class QiNiuUpload {

	@Autowired
	static CloudStorageConfig qnconfig;
	
	//上传一个图片，上传头像
	public static String upload(MultipartFile file) throws IOException {
        //七牛的配置信息
        UploadManager qiniu= new UploadManager(new Configuration(Zone.zone2()));
        Auth auth = Auth.create(qnconfig.getQiniuAccessKey(), qnconfig.getQiniuSecretKey());
        String token = auth.uploadToken(qnconfig.getQiniuBucketName());
        //获取文件名 注意要确保唯一性
        String fileName = file.getOriginalFilename();
        
        if (!file.isEmpty()) {
                FileInputStream inputStream = (FileInputStream) file.getInputStream();            
                try{
                    // 上传图片文件
                    Response res = qiniu.put(inputStream, fileName, token,null,null);
                    if (!res.isOK()) {
                        throw new RuntimeException("上传七牛出错：" + res.toString());
                    }
                    // 解析上传成功的结果
                    ObjectMapper gson = new  ObjectMapper();
                    DefaultPutRet putRet = gson.readValue(res.bodyString(), DefaultPutRet.class);
                    String path = qnconfig.getQiniuDomain() + putRet.key;
                    // 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
                    System.out.print("七牛云返回的图片链接:" + path);              
                    return path;
                }catch (QiniuException e){
                    e.printStackTrace();
                }
      }    
      return null;
    }
	
	//上传多个图片，上传房源图片
	public static String upManyImg(List<MultipartFile> files) throws IOException {
		//用来放多个图片的字符串
		Map<String,String> imgs = new HashMap<String,String>();
		int n = 1;
        //七牛的配置信息
        UploadManager qiniu= new UploadManager(new Configuration(Zone.zone2()));
        System.out.println(qnconfig.getQiniuAccessKey()+"--"+qnconfig.getQiniuSecretKey());
        Auth auth = Auth.create(qnconfig.getQiniuAccessKey(), qnconfig.getQiniuSecretKey());
        String token = auth.uploadToken(qnconfig.getQiniuBucketName());
        for(MultipartFile file : files) {
	        //获取文件名 注意要确保唯一性
	        String fileName = file.getOriginalFilename();
	        if (!file.isEmpty()) {
	                FileInputStream inputStream = (FileInputStream) file.getInputStream();            
	                try{
	                    // 上传图片文件
	                    Response res = qiniu.put(inputStream, fileName, token,null,null);
	                    if (!res.isOK()) {
	                        throw new RuntimeException("上传七牛出错：" + res.toString());
	                    }
	                    // 解析上传成功的结果
	                    ObjectMapper gson = new  ObjectMapper();
	                    DefaultPutRet putRet = gson.readValue(res.bodyString(), DefaultPutRet.class);
	                    String path = qnconfig.getQiniuDomain() + putRet.key;
	                    // 这个returnPath是获得到的外链地址,通过这个地址可以直接打开图片
	                    System.out.print("七牛云返回的图片链接:" + path);   
	                    imgs.put("img"+n++, path);
	                }catch (QiniuException e){
	                    e.printStackTrace();
	                }
	      }
        }
        String json = JSONObject.toJSONString(imgs);
        return json;
    }
}
